/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZSearchParams;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.soap.type.SearchSortBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLongSearchResultSets
/*     */   extends TestCase
/*     */ {
/*  41 */   private static final String NAME_PREFIX = TestLongSearchResultSets.class.getSimpleName();
/*     */   private static final String RECIPIENT = "search_test_1";
/*     */   private static final String RECIPIENT2 = "search_test_2";
/*     */   private static final String SENDER = "user1";
/*     */   private static final String PASSWORD = "test123";
/*     */   private static final String SEARCH_STRING = "jkfheowerklsaklasdqweds";
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  50 */     cleanUp();
/*     */     
/*  52 */     Map<String, Object> attrs = new HashMap();
/*  53 */     attrs.put("zimbraMailHost", LC.zimbra_server_hostname.value());
/*  54 */     attrs.put("zimbraAttachmentsIndexingEnabled", Boolean.valueOf(true));
/*  55 */     attrs.put("cn", "search_test1");
/*  56 */     attrs.put("displayName", "TestAccount unit test user 1");
/*  57 */     Provisioning.getInstance().createAccount(TestUtil.getAddress("search_test_1"), "test123", attrs);
/*     */     
/*  59 */     attrs = new HashMap();
/*  60 */     attrs.put("zimbraMailHost", LC.zimbra_server_hostname.value());
/*  61 */     attrs.put("zimbraAttachmentsIndexingEnabled", Boolean.valueOf(false));
/*  62 */     attrs.put("cn", "search_tes2t");
/*  63 */     attrs.put("displayName", "TestAccount unit test user 2");
/*  64 */     Provisioning.getInstance().createAccount(TestUtil.getAddress("search_test_2"), "test123", attrs);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testConversations() {
/*  69 */     try { Mailbox recieverMbox = TestUtil.getMailbox("search_test_1");
/*  70 */       long timestamp = System.currentTimeMillis() - 216000000L;
/*  71 */       int numMessages = 3207;
/*  72 */       int firstLimit = 100;
/*  73 */       int incLimit = 50;
/*  74 */       int offset = 0;
/*  75 */       ArrayList<String> expectedIds = new ArrayList();
/*  76 */       for (int i = 0; i < numMessages; i++) {
/*  77 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/*  78 */         expectedIds.add(Integer.toString(msg.getId()));
/*  79 */         timestamp += 1000L;
/*  80 */         Thread.sleep(100L);
/*     */       }
/*  82 */       Collections.reverse(expectedIds);
/*  83 */       Thread.sleep(100L);
/*  84 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_1");
/*  85 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/*  86 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/*  87 */       searchParams.setLimit(firstLimit);
/*  88 */       searchParams.setTypes("conversation");
/*  89 */       List<String> msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/*  90 */       assertEquals(firstLimit, msgIds.size());
/*  91 */       int gotMessages = msgIds.size();
/*  92 */       List<String> seenIds = new ArrayList();
/*  93 */       seenIds.addAll(msgIds);
/*  94 */       int recCount = 1;
/*  95 */       offset += firstLimit;
/*     */       
/*  97 */       while (gotMessages > 0) {
/*  98 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/*  99 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 100 */         searchParams.setLimit(incLimit);
/* 101 */         searchParams.setTypes("conversation");
/* 102 */         searchParams.setOffset(offset);
/* 103 */         msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 104 */         recCount++;
/* 105 */         gotMessages = msgIds.size();
/* 106 */         int resCount = 0;
/* 107 */         for (String szId : msgIds) {
/* 108 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), szId, Integer.valueOf(seenIds.indexOf(szId)) }), seenIds.contains(szId));
/* 109 */           seenIds.add(szId);
/* 110 */           resCount++;
/*     */         }
/* 112 */         Thread.sleep(100L);
/* 113 */         offset += incLimit;
/*     */       }
/* 115 */       assertEquals("Returned incorrect number of conversations", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 117 */       e.printStackTrace();
/* 118 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 120 */       e.printStackTrace();
/* 121 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testConversations2() {
/*     */     try {
/* 128 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_2");
/* 129 */       long timestamp = System.currentTimeMillis() - 216000000L;
/* 130 */       int numMessages = 3207;
/* 131 */       int firstLimit = 100;
/* 132 */       int incLimit = 50;
/* 133 */       int offset = 0;
/* 134 */       ArrayList<String> expectedIds = new ArrayList();
/* 135 */       for (int i = 0; i < numMessages; i++) {
/* 136 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_2"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/* 137 */         expectedIds.add(Integer.toString(msg.getId()));
/* 138 */         timestamp += 1000L;
/* 139 */         Thread.sleep(100L);
/*     */       }
/* 141 */       Collections.reverse(expectedIds);
/* 142 */       Thread.sleep(100L);
/* 143 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_2");
/* 144 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 145 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 146 */       searchParams.setLimit(firstLimit);
/* 147 */       searchParams.setTypes("conversation");
/* 148 */       List<String> msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 149 */       assertEquals(firstLimit, msgIds.size());
/* 150 */       int gotMessages = msgIds.size();
/* 151 */       List<String> seenIds = new ArrayList();
/* 152 */       seenIds.addAll(msgIds);
/* 153 */       int recCount = 1;
/* 154 */       offset += firstLimit;
/* 155 */       while (gotMessages > 0) {
/* 156 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 157 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 158 */         searchParams.setLimit(incLimit);
/* 159 */         searchParams.setTypes("conversation");
/* 160 */         searchParams.setOffset(offset);
/* 161 */         msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 162 */         recCount++;
/* 163 */         gotMessages = msgIds.size();
/* 164 */         int resCount = 0;
/* 165 */         for (String szId : msgIds) {
/* 166 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), szId, Integer.valueOf(seenIds.indexOf(szId)) }), seenIds.contains(szId));
/* 167 */           seenIds.add(szId);
/* 168 */           resCount++;
/*     */         }
/* 170 */         Thread.sleep(100L);
/* 171 */         offset += incLimit;
/*     */       }
/* 173 */       assertEquals("Returned incorrect number of conversations", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 175 */       e.printStackTrace();
/* 176 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 178 */       e.printStackTrace();
/* 179 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessages()
/*     */   {
/*     */     try {
/* 187 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_1");
/* 188 */       long timestamp = System.currentTimeMillis() - 216000000L;
/* 189 */       int numMessages = 2257;
/* 190 */       int firstLimit = 100;
/* 191 */       int incLimit = 50;
/* 192 */       int offset = 0;
/* 193 */       ArrayList<String> expectedIds = new ArrayList();
/* 194 */       for (int i = 0; i < numMessages; i++) {
/* 195 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/* 196 */         expectedIds.add(Integer.toString(msg.getId()));
/* 197 */         timestamp += 1000L;
/* 198 */         Thread.sleep(100L);
/*     */       }
/* 200 */       Collections.reverse(expectedIds);
/* 201 */       Thread.sleep(100L);
/* 202 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_1");
/* 203 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 204 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 205 */       searchParams.setLimit(firstLimit);
/* 206 */       searchParams.setTypes("message");
/* 207 */       List<String> msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 208 */       assertEquals(firstLimit, msgIds.size());
/* 209 */       int gotMessages = msgIds.size();
/* 210 */       List<String> seenIds = new ArrayList();
/* 211 */       seenIds.addAll(msgIds);
/* 212 */       int recCount = 1;
/* 213 */       offset += firstLimit;
/* 214 */       while (gotMessages > 0) {
/* 215 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 216 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 217 */         searchParams.setLimit(incLimit);
/* 218 */         searchParams.setTypes("message");
/* 219 */         searchParams.setOffset(offset);
/* 220 */         msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 221 */         recCount++;
/* 222 */         gotMessages = msgIds.size();
/* 223 */         int resCount = 0;
/* 224 */         for (String szId : msgIds) {
/* 225 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), szId, Integer.valueOf(seenIds.indexOf(szId)) }), seenIds.contains(szId));
/* 226 */           seenIds.add(szId);
/* 227 */           resCount++;
/*     */         }
/* 229 */         Thread.sleep(100L);
/* 230 */         offset += incLimit;
/*     */       }
/* 232 */       for (int i = 0; i < expectedIds.size(); i++) {
/* 233 */         assertEquals("IDs at index " + i + " do not match", (String)expectedIds.get(i), (String)seenIds.get(i));
/*     */       }
/* 235 */       assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 237 */       e.printStackTrace();
/* 238 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 240 */       e.printStackTrace();
/* 241 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessages2()
/*     */   {
/*     */     try {
/* 249 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_2");
/* 250 */       long timestamp = System.currentTimeMillis() - 216000000L;
/* 251 */       int numMessages = 2257;
/* 252 */       int firstLimit = 100;
/* 253 */       int incLimit = 50;
/* 254 */       int offset = 0;
/* 255 */       ArrayList<String> expectedIds = new ArrayList();
/* 256 */       for (int i = 0; i < numMessages; i++) {
/* 257 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_2"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/* 258 */         expectedIds.add(Integer.toString(msg.getId()));
/* 259 */         timestamp += 1000L;
/* 260 */         Thread.sleep(100L);
/*     */       }
/* 262 */       Collections.reverse(expectedIds);
/* 263 */       Thread.sleep(100L);
/* 264 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_2");
/* 265 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 266 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 267 */       searchParams.setLimit(firstLimit);
/* 268 */       searchParams.setTypes("message");
/* 269 */       List<String> msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 270 */       assertEquals(firstLimit, msgIds.size());
/* 271 */       int gotMessages = msgIds.size();
/* 272 */       List<String> seenIds = new ArrayList();
/* 273 */       seenIds.addAll(msgIds);
/* 274 */       int recCount = 1;
/* 275 */       offset += firstLimit;
/* 276 */       while (gotMessages > 0) {
/* 277 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 278 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 279 */         searchParams.setLimit(incLimit);
/* 280 */         searchParams.setTypes("message");
/* 281 */         searchParams.setOffset(offset);
/* 282 */         msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 283 */         recCount++;
/* 284 */         gotMessages = msgIds.size();
/* 285 */         int resCount = 0;
/* 286 */         for (String szId : msgIds) {
/* 287 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), szId, Integer.valueOf(seenIds.indexOf(szId)) }), seenIds.contains(szId));
/* 288 */           seenIds.add(szId);
/* 289 */           resCount++;
/*     */         }
/* 291 */         Thread.sleep(100L);
/* 292 */         offset += incLimit;
/*     */       }
/* 294 */       for (int i = 0; i < expectedIds.size(); i++) {
/* 295 */         assertEquals("IDs at index " + i + " do not match", (String)expectedIds.get(i), (String)seenIds.get(i));
/*     */       }
/* 297 */       assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 299 */       e.printStackTrace();
/* 300 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 302 */       e.printStackTrace();
/* 303 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 311 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 315 */     Provisioning prov = Provisioning.getInstance();
/* 316 */     Account account = prov.get(Key.AccountBy.name, TestUtil.getAddress("search_test_1"));
/* 317 */     if (account != null) {
/* 318 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */     
/* 321 */     account = prov.get(Key.AccountBy.name, TestUtil.getAddress("search_test_2"));
/* 322 */     if (account != null) {
/* 323 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestLongSearchResultSets.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */